package com.kaibes.module.encryption;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kaibes.core.spring.proxy.Proxy;
import com.kaibes.web.api.ApiLink;
import com.kaibes.web.encryption.AesKey;
import com.kaibes.web.encryption.EncryptionKey;
import com.kaibes.web.encryption.EncryptionKeyService;
import com.kaibes.web.encryption.EncryptionUtils;
import com.kaibes.web.encryption.RsaKey;
import com.kaibes.web.response.ResponseFactory;

@RestController
@RequestMapping("/encryptions")
public class EncryptionApi {

    @Proxy
    private EncryptionKeyService keyService;
    
    @GetMapping
    @ApiLink(value = "获取秘钥信息", validated = false)
    public ResponseEntity<EncryptionKey> get() {
        EncryptionKey encryption = keyService.getObject(EncryptionKey.class);
        return ResponseFactory.post(encryption, "获取秘钥信息成功", "获取秘钥信息失败");
    } 

    @PostMapping("/rsa")
    @ApiLink("随机生成RSA密钥")
    public ResponseEntity<Object> postRsa() {
        EncryptionKey encryption = keyService.getObject(EncryptionKey.class);
        RsaKey temp = EncryptionUtils.createRsaKey();
        encryption.setRsaKey(temp);
        keyService.saveObject(encryption);
        return ResponseFactory.post(temp, "生成RSA密钥成功", "生成RSA密钥失败");
    }

    @PostMapping("/aes")
    @ApiLink("随机生成aes秘钥")
    public ResponseEntity<Object> postAes() {
        EncryptionKey encryption = keyService.getObject(EncryptionKey.class);
        AesKey temp = EncryptionUtils.createAesKey();
        encryption.setAesKey(temp);
        keyService.saveObject(encryption);
        return ResponseFactory.post(temp, "生成AES密钥成功", "生成AES密钥失败");
    }

}
